﻿Imports System.Linq
Imports System.Data
Imports System.Web
Imports log4net
Imports Equipment.DataAccess

Public Class UserNotificationEmail


#Region "Public Section"
    Private Shared logger As ILog = LogManager.GetLogger(GetType(UserNotificationEmail))

    Public Shared Function RetrieveAllByDate(ByVal tillDate As DateTime) As IEnumerable(Of UserNotificationEmail)
        Return From ne In Database.Instance.UserNotificationEmails Where ne.WaitTillDate <= tillDate
    End Function

    Public Shared Sub Insert(ByVal toUser As User, ByVal notificationID As Integer, ByVal name As String, ByVal waitTillDate As DateTime)

        Dim _dataBase As EquipmentDataContext = Database.Instance
        Dim newEmail As New UserNotificationEmail

        newEmail.UserID = toUser.ID
        newEmail.NotificationID = notificationID
        newEmail.EntityName = name
        newEmail.WaitTillDate = waitTillDate

        _dataBase.UserNotificationEmails.InsertOnSubmit(newEmail)

        Try
            _dataBase.SubmitChanges()
        Catch ex As Exception
            logger.Error(ex.Message + ":" + ex.StackTrace)
        End Try

    End Sub


    Public Sub Delete()

        Dim db As EquipmentDataContext = Database.Instance
        Dim result = From e In db.UserNotificationEmails Where e.ID = Me.ID
        Dim deletingEntity As UserNotificationEmail = result.FirstOrDefault()

        db.UserNotificationEmails.DeleteOnSubmit(deletingEntity)

        Try
            db.SubmitChanges()

        Catch ex As Exception
            logger.Error(ex.Message + ":" + ex.StackTrace)
        End Try

    End Sub
#End Region
End Class
